<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/sure3.css" rel="stylesheet" type="text/css"/>
    <link href="../Styles/base1.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div class="markdown-body entry-content" itemprop="text">
    <h1><a href="#set-和-map-数据结构"  class="anchor" id="user-content-set-和-map-数据结构">

    </a>Set 和 Map 数据结构
    </h1>
    <h2><a href="#set"  class="anchor" id="user-content-set">

    </a>Set
    </h2>
    <h3><a href="#基本用法"  class="anchor" id="user-content-基本用法">

    </a>基本用法
    </h3>
    <p>ES6 提供了新的数据结构 Set。它类似于数组，但是成员的值都是唯一的，没有重复的值。</p>
    <p>Set 本身是一个构造函数，用来生成 Set 数据结构。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">s</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Set</span>();

[<span class="pl-c1">2</span>, <span class="pl-c1">3</span>, <span class="pl-c1">5</span>, <span class="pl-c1">4</span>, <span
                class="pl-c1">5</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>].<span class="pl-c1">forEach</span>(<span
                class="pl-smi">x</span> <span class="pl-k">=&gt;</span> <span class="pl-smi">s</span>.<span
                class="pl-c1">add</span>(x));

<span class="pl-k">for</span> (<span class="pl-k">let</span> i <span class="pl-k">of</span> s) {
  <span class="pl-en">console</span>.<span class="pl-c1">log</span>(i);
}
<span class="pl-c"><span class="pl-c">//</span> 2 3 5 4</span></pre>
    </div>
    <p>上面代码通过<code>add</code>方法向 Set 结构加入成员，结果表明 Set 结构不会添加重复的值。</p>
    <p>Set 函数可以接受一个数组（或者具有 iterable 接口的其他数据结构）作为参数，用来初始化。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c"><span class="pl-c">//</span> 例一</span>
<span class="pl-k">const</span> <span class="pl-c1">set</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">Set</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span
                class="pl-c1">3</span>, <span class="pl-c1">4</span>, <span class="pl-c1">4</span>]);
[<span class="pl-k">...</span>set]
<span class="pl-c"><span class="pl-c">//</span> [1, 2, 3, 4]</span>

<span class="pl-c"><span class="pl-c">//</span> 例二</span>
<span class="pl-k">const</span> <span class="pl-c1">items</span> <span class="pl-k">=</span> <span
                class="pl-k">new</span> <span class="pl-en">Set</span>([<span class="pl-c1">1</span>, <span
                class="pl-c1">2</span>, <span class="pl-c1">3</span>, <span class="pl-c1">4</span>, <span class="pl-c1">5</span>, <span
                class="pl-c1">5</span>, <span class="pl-c1">5</span>, <span class="pl-c1">5</span>]);
<span class="pl-smi">items</span>.<span class="pl-c1">size</span> <span class="pl-c"><span
                class="pl-c">//</span> 5</span>

<span class="pl-c"><span class="pl-c">//</span> 例三</span>
<span class="pl-k">function</span> <span class="pl-en">divs</span> () {
  <span class="pl-k">return</span> [<span class="pl-k">...</span><span class="pl-c1">document</span>.<span
                class="pl-c1">querySelectorAll</span>(<span class="pl-s"><span class="pl-pds">'</span>div<span
                class="pl-pds">'</span></span>)];
}

<span class="pl-k">const</span> <span class="pl-c1">set</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">Set</span>(<span class="pl-en">divs</span>());
<span class="pl-smi">set</span>.<span class="pl-c1">size</span> <span class="pl-c"><span
                class="pl-c">//</span> 56</span>

<span class="pl-c"><span class="pl-c">//</span> 类似于</span>
<span class="pl-en">divs</span>().<span class="pl-c1">forEach</span>(<span class="pl-smi">div</span> <span class="pl-k">=&gt;</span> <span
                class="pl-smi">set</span>.<span class="pl-c1">add</span>(div));
<span class="pl-smi">set</span>.<span class="pl-c1">size</span> <span class="pl-c"><span
                class="pl-c">//</span> 56</span></pre>
    </div>
    <p>上面代码中，例一和例二都是<code>Set</code>函数接受数组作为参数，例三是接受类似数组的对象作为参数。</p>
    <p>上面代码中，也展示了一种去除数组重复成员的方法。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c"><span class="pl-c">//</span> 去除数组的重复成员</span>
[<span class="pl-k">...</span><span class="pl-k">new</span> <span class="pl-en">Set</span>(array)]</pre>
    </div>
    <p>向 Set 加入值的时候，不会发生类型转换，所以<code>5</code>和<code>"5"</code>是两个不同的值。Set 内部判断两个值是否不同，使用的算法叫做“Same-value
        equality”，它类似于精确相等运算符（<code>===</code>），主要的区别是<code>NaN</code>等于自身，而精确相等运算符认为<code>NaN</code>不等于自身。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">let</span> set <span class="pl-k">=</span> <span
            class="pl-k">new</span> <span class="pl-en">Set</span>();
<span class="pl-k">let</span> a <span class="pl-k">=</span> <span class="pl-c1">NaN</span>;
<span class="pl-k">let</span> b <span class="pl-k">=</span> <span class="pl-c1">NaN</span>;
<span class="pl-smi">set</span>.<span class="pl-c1">add</span>(a);
<span class="pl-smi">set</span>.<span class="pl-c1">add</span>(b);
set <span class="pl-c"><span class="pl-c">//</span> Set {NaN}</span></pre>
    </div>
    <p>上面代码向 Set 实例添加了两个<code>NaN</code>，但是只能加入一个。这表明，在 Set 内部，两个<code>NaN</code>是相等。</p>
    <p>另外，两个对象总是不相等的。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">let</span> set <span class="pl-k">=</span> <span
            class="pl-k">new</span> <span class="pl-en">Set</span>();

<span class="pl-smi">set</span>.<span class="pl-c1">add</span>({});
<span class="pl-smi">set</span>.<span class="pl-c1">size</span> <span class="pl-c"><span class="pl-c">//</span> 1</span>

<span class="pl-smi">set</span>.<span class="pl-c1">add</span>({});
<span class="pl-smi">set</span>.<span class="pl-c1">size</span> <span class="pl-c"><span class="pl-c">//</span> 2</span></pre>
    </div>
    <p>上面代码表示，由于两个空对象不相等，所以它们被视为两个值。</p>
    <h3><a href="#set-实例的属性和方法"  class="anchor" id="user-content-set-实例的属性和方法">

    </a>Set 实例的属性和方法
    </h3>
    <p>Set 结构的实例有以下属性。</p>
    <ul>
        <li><code>Set.prototype.constructor</code>：构造函数，默认就是<code>Set</code>函数。</li>
        <li><code>Set.prototype.size</code>：返回<code>Set</code>实例的成员总数。</li>
    </ul>
    <p>Set 实例的方法分为两大类：操作方法（用于操作数据）和遍历方法（用于遍历成员）。下面先介绍四个操作方法。</p>
    <ul>
        <li><code>add(value)</code>：添加某个值，返回 Set 结构本身。</li>
        <li><code>delete(value)</code>：删除某个值，返回一个布尔值，表示删除是否成功。</li>
        <li><code>has(value)</code>：返回一个布尔值，表示该值是否为<code>Set</code>的成员。</li>
        <li><code>clear()</code>：清除所有成员，没有返回值。</li>
    </ul>
    <p>上面这些属性和方法的实例如下。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-smi">s</span>.<span class="pl-c1">add</span>(<span
            class="pl-c1">1</span>).<span class="pl-c1">add</span>(<span class="pl-c1">2</span>).<span
            class="pl-c1">add</span>(<span class="pl-c1">2</span>);
<span class="pl-c"><span class="pl-c">//</span> 注意2被加入了两次</span>

<span class="pl-smi">s</span>.<span class="pl-c1">size</span> <span class="pl-c"><span class="pl-c">//</span> 2</span>

<span class="pl-smi">s</span>.<span class="pl-c1">has</span>(<span class="pl-c1">1</span>) <span class="pl-c"><span
                class="pl-c">//</span> true</span>
<span class="pl-smi">s</span>.<span class="pl-c1">has</span>(<span class="pl-c1">2</span>) <span class="pl-c"><span
                class="pl-c">//</span> true</span>
<span class="pl-smi">s</span>.<span class="pl-c1">has</span>(<span class="pl-c1">3</span>) <span class="pl-c"><span
                class="pl-c">//</span> false</span>

<span class="pl-smi">s</span>.<span class="pl-c1">delete</span>(<span class="pl-c1">2</span>);
<span class="pl-smi">s</span>.<span class="pl-c1">has</span>(<span class="pl-c1">2</span>) <span class="pl-c"><span
                class="pl-c">//</span> false</span></pre>
    </div>
    <p>下面是一个对比，看看在判断是否包括一个键上面，<code>Object</code>结构和<code>Set</code>结构的写法不同。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c"><span class="pl-c">//</span> 对象的写法</span>
<span class="pl-k">const</span> <span class="pl-c1">properties</span> <span class="pl-k">=</span> {
  <span class="pl-s"><span class="pl-pds">'</span>width<span class="pl-pds">'</span></span><span
                class="pl-k">:</span> <span class="pl-c1">1</span>,
  <span class="pl-s"><span class="pl-pds">'</span>height<span class="pl-pds">'</span></span><span class="pl-k">:</span> <span
                class="pl-c1">1</span>
};

<span class="pl-k">if</span> (properties[someName]) {
  <span class="pl-c"><span class="pl-c">//</span> do something</span>
}

<span class="pl-c"><span class="pl-c">//</span> Set的写法</span>
<span class="pl-k">const</span> <span class="pl-c1">properties</span> <span class="pl-k">=</span> <span
                class="pl-k">new</span> <span class="pl-en">Set</span>();

<span class="pl-smi">properties</span>.<span class="pl-c1">add</span>(<span class="pl-s"><span class="pl-pds">'</span>width<span
                class="pl-pds">'</span></span>);
<span class="pl-smi">properties</span>.<span class="pl-c1">add</span>(<span class="pl-s"><span class="pl-pds">'</span>height<span
                class="pl-pds">'</span></span>);

<span class="pl-k">if</span> (<span class="pl-smi">properties</span>.<span class="pl-c1">has</span>(someName)) {
  <span class="pl-c"><span class="pl-c">//</span> do something</span>
}</pre>
    </div>
    <p><code>Array.from</code>方法可以将 Set 结构转为数组。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span
            class="pl-c1">items</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span
            class="pl-en">Set</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span
            class="pl-c1">3</span>, <span class="pl-c1">4</span>, <span class="pl-c1">5</span>]);
<span class="pl-k">const</span> <span class="pl-c1">array</span> <span class="pl-k">=</span> <span
                class="pl-c1">Array</span>.<span class="pl-en">from</span>(items);</pre>
    </div>
    <p>这就提供了去除数组重复成员的另一种方法。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">function</span> <span class="pl-en">dedupe</span>(<span
            class="pl-smi">array</span>) {
  <span class="pl-k">return</span> <span class="pl-c1">Array</span>.<span class="pl-en">from</span>(<span class="pl-k">new</span> <span
                class="pl-en">Set</span>(array));
}

<span class="pl-en">dedupe</span>([<span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span
                class="pl-c1">2</span>, <span class="pl-c1">3</span>]) <span class="pl-c"><span class="pl-c">//</span> [1, 2, 3]</span></pre>
    </div>
    <h3><a href="#遍历操作"  class="anchor" id="user-content-遍历操作">

    </a>遍历操作
    </h3>
    <p>Set 结构的实例有四个遍历方法，可以用于遍历成员。</p>
    <ul>
        <li><code>keys()</code>：返回键名的遍历器</li>
        <li><code>values()</code>：返回键值的遍历器</li>
        <li><code>entries()</code>：返回键值对的遍历器</li>
        <li><code>forEach()</code>：使用回调函数遍历每个成员</li>
    </ul>
    <p>需要特别指出的是，<code>Set</code>的遍历顺序就是插入顺序。这个特性有时非常有用，比如使用 Set 保存一个回调函数列表，调用时就能保证按照添加顺序调用。</p>
    <p><strong>（1）<code>keys()</code>，<code>values()</code>，<code>entries()</code></strong></p>
    <p><code>keys</code>方法、<code>values</code>方法、<code>entries</code>方法返回的都是遍历器对象（详见《Iterator 对象》一章）。由于 Set
        结构没有键名，只有键值（或者说键名和键值是同一个值），所以<code>keys</code>方法和<code>values</code>方法的行为完全一致。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">let</span> set <span class="pl-k">=</span> <span
            class="pl-k">new</span> <span class="pl-en">Set</span>([<span class="pl-s"><span
            class="pl-pds">'</span>red<span class="pl-pds">'</span></span>, <span class="pl-s"><span
            class="pl-pds">'</span>green<span class="pl-pds">'</span></span>, <span class="pl-s"><span
            class="pl-pds">'</span>blue<span class="pl-pds">'</span></span>]);

<span class="pl-k">for</span> (<span class="pl-k">let</span> item <span class="pl-k">of</span> <span
                class="pl-smi">set</span>.<span class="pl-c1">keys</span>()) {
  <span class="pl-en">console</span>.<span class="pl-c1">log</span>(item);
}
<span class="pl-c"><span class="pl-c">//</span> red</span>
<span class="pl-c"><span class="pl-c">//</span> green</span>
<span class="pl-c"><span class="pl-c">//</span> blue</span>

<span class="pl-k">for</span> (<span class="pl-k">let</span> item <span class="pl-k">of</span> <span
                class="pl-smi">set</span>.<span class="pl-c1">values</span>()) {
  <span class="pl-en">console</span>.<span class="pl-c1">log</span>(item);
}
<span class="pl-c"><span class="pl-c">//</span> red</span>
<span class="pl-c"><span class="pl-c">//</span> green</span>
<span class="pl-c"><span class="pl-c">//</span> blue</span>

<span class="pl-k">for</span> (<span class="pl-k">let</span> item <span class="pl-k">of</span> <span
                class="pl-smi">set</span>.<span class="pl-c1">entries</span>()) {
  <span class="pl-en">console</span>.<span class="pl-c1">log</span>(item);
}
<span class="pl-c"><span class="pl-c">//</span> ["red", "red"]</span>
<span class="pl-c"><span class="pl-c">//</span> ["green", "green"]</span>
<span class="pl-c"><span class="pl-c">//</span> ["blue", "blue"]</span></pre>
    </div>
    <p>上面代码中，<code>entries</code>方法返回的遍历器，同时包括键名和键值，所以每次输出一个数组，它的两个成员完全相等。</p>
    <p>Set 结构的实例默认可遍历，它的默认遍历器生成函数就是它的<code>values</code>方法。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Set</span>.<span class="pl-c1">prototype</span>[<span
            class="pl-c1">Symbol</span>.<span class="pl-smi">iterator</span>] <span class="pl-k">===</span> <span
            class="pl-c1">Set</span>.<span class="pl-c1">prototype</span>.<span class="pl-smi">values</span>
<span class="pl-c"><span class="pl-c">//</span> true</span></pre>
    </div>
    <p>这意味着，可以省略<code>values</code>方法，直接用<code>for...of</code>循环遍历 Set。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">let</span> set <span class="pl-k">=</span> <span
            class="pl-k">new</span> <span class="pl-en">Set</span>([<span class="pl-s"><span
            class="pl-pds">'</span>red<span class="pl-pds">'</span></span>, <span class="pl-s"><span
            class="pl-pds">'</span>green<span class="pl-pds">'</span></span>, <span class="pl-s"><span
            class="pl-pds">'</span>blue<span class="pl-pds">'</span></span>]);

<span class="pl-k">for</span> (<span class="pl-k">let</span> x <span class="pl-k">of</span> set) {
  <span class="pl-en">console</span>.<span class="pl-c1">log</span>(x);
}
<span class="pl-c"><span class="pl-c">//</span> red</span>
<span class="pl-c"><span class="pl-c">//</span> green</span>
<span class="pl-c"><span class="pl-c">//</span> blue</span></pre>
    </div>
    <p><strong>（2）<code>forEach()</code></strong></p>
    <p>Set 结构的实例与数组一样，也拥有<code>forEach</code>方法，用于对每个成员执行某种操作，没有返回值。</p>
    <div class="highlight highlight-source-js"><pre>set <span class="pl-k">=</span> <span class="pl-k">new</span> <span
            class="pl-en">Set</span>([<span class="pl-c1">1</span>, <span class="pl-c1">4</span>, <span
            class="pl-c1">9</span>]);
<span class="pl-smi">set</span>.<span class="pl-c1">forEach</span>((<span class="pl-smi">value</span>, <span
                class="pl-smi">key</span>) <span class="pl-k">=&gt;</span> <span class="pl-en">console</span>.<span
                class="pl-c1">log</span>(key <span class="pl-k">+</span> <span class="pl-s"><span
                class="pl-pds">'</span> : <span class="pl-pds">'</span></span> <span class="pl-k">+</span> value))
<span class="pl-c"><span class="pl-c">//</span> 1 : 1</span>
<span class="pl-c"><span class="pl-c">//</span> 4 : 4</span>
<span class="pl-c"><span class="pl-c">//</span> 9 : 9</span></pre>
    </div>
    <p>上面代码说明，<code>forEach</code>方法的参数就是一个处理函数。该函数的参数与数组的<code>forEach</code>一致，依次为键值、键名、集合本身（上例省略了该参数）。这里需要注意，Set
        结构的键名就是键值（两者是同一个值），因此第一个参数与第二个参数的值永远都是一样的。</p>
    <p>另外，<code>forEach</code>方法还可以有第二个参数，表示绑定处理函数内部的<code>this</code>对象。</p>
    <p><strong>（3）遍历的应用</strong></p>
    <p>扩展运算符（<code>...</code>）内部使用<code>for...of</code>循环，所以也可以用于 Set 结构。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">let</span> set <span class="pl-k">=</span> <span
            class="pl-k">new</span> <span class="pl-en">Set</span>([<span class="pl-s"><span
            class="pl-pds">'</span>red<span class="pl-pds">'</span></span>, <span class="pl-s"><span
            class="pl-pds">'</span>green<span class="pl-pds">'</span></span>, <span class="pl-s"><span
            class="pl-pds">'</span>blue<span class="pl-pds">'</span></span>]);
<span class="pl-k">let</span> arr <span class="pl-k">=</span> [<span class="pl-k">...</span>set];
<span class="pl-c"><span class="pl-c">//</span> ['red', 'green', 'blue']</span></pre>
    </div>
    <p>扩展运算符和 Set 结构相结合，就可以去除数组的重复成员。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">let</span> arr <span class="pl-k">=</span> [<span
            class="pl-c1">3</span>, <span class="pl-c1">5</span>, <span class="pl-c1">2</span>, <span
            class="pl-c1">2</span>, <span class="pl-c1">5</span>, <span class="pl-c1">5</span>];
<span class="pl-k">let</span> unique <span class="pl-k">=</span> [<span class="pl-k">...</span><span
                class="pl-k">new</span> <span class="pl-en">Set</span>(arr)];
<span class="pl-c"><span class="pl-c">//</span> [3, 5, 2]</span></pre>
    </div>
    <p>而且，数组的<code>map</code>和<code>filter</code>方法也可以用于 Set 了。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">let</span> set <span class="pl-k">=</span> <span
            class="pl-k">new</span> <span class="pl-en">Set</span>([<span class="pl-c1">1</span>, <span
            class="pl-c1">2</span>, <span class="pl-c1">3</span>]);
set <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Set</span>([<span
                class="pl-k">...</span>set].<span class="pl-en">map</span>(<span class="pl-smi">x</span> <span
                class="pl-k">=&gt;</span> x <span class="pl-k">*</span> <span class="pl-c1">2</span>));
<span class="pl-c"><span class="pl-c">//</span> 返回Set结构：{2, 4, 6}</span>

<span class="pl-k">let</span> set <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">Set</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span
                class="pl-c1">3</span>, <span class="pl-c1">4</span>, <span class="pl-c1">5</span>]);
set <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Set</span>([<span
                class="pl-k">...</span>set].<span class="pl-en">filter</span>(<span class="pl-smi">x</span> <span
                class="pl-k">=&gt;</span> (x <span class="pl-k">%</span> <span class="pl-c1">2</span>) <span
                class="pl-k">==</span> <span class="pl-c1">0</span>));
<span class="pl-c"><span class="pl-c">//</span> 返回Set结构：{2, 4}</span></pre>
    </div>
    <p>因此使用 Set 可以很容易地实现并集（Union）、交集（Intersect）和差集（Difference）。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">let</span> a <span class="pl-k">=</span> <span
            class="pl-k">new</span> <span class="pl-en">Set</span>([<span class="pl-c1">1</span>, <span
            class="pl-c1">2</span>, <span class="pl-c1">3</span>]);
<span class="pl-k">let</span> b <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Set</span>([<span
                class="pl-c1">4</span>, <span class="pl-c1">3</span>, <span class="pl-c1">2</span>]);

<span class="pl-c"><span class="pl-c">//</span> 并集</span>
<span class="pl-k">let</span> union <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">Set</span>([<span class="pl-k">...</span>a, <span class="pl-k">...</span>b]);
<span class="pl-c"><span class="pl-c">//</span> Set {1, 2, 3, 4}</span>

<span class="pl-c"><span class="pl-c">//</span> 交集</span>
<span class="pl-k">let</span> intersect <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Set</span>([<span
                class="pl-k">...</span>a].<span class="pl-en">filter</span>(<span class="pl-smi">x</span> <span
                class="pl-k">=&gt;</span> <span class="pl-smi">b</span>.<span class="pl-c1">has</span>(x)));
<span class="pl-c"><span class="pl-c">//</span> set {2, 3}</span>

<span class="pl-c"><span class="pl-c">//</span> 差集</span>
<span class="pl-k">let</span> difference <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Set</span>([<span
                class="pl-k">...</span>a].<span class="pl-en">filter</span>(<span class="pl-smi">x</span> <span
                class="pl-k">=&gt;</span> <span class="pl-k">!</span><span class="pl-smi">b</span>.<span class="pl-c1">has</span>(x)));
<span class="pl-c"><span class="pl-c">//</span> Set {1}</span></pre>
    </div>
    <p>如果想在遍历操作中，同步改变原来的 Set 结构，目前没有直接的方法，但有两种变通方法。一种是利用原 Set 结构映射出一个新的结构，然后赋值给原来的 Set 结构；另一种是利用<code>Array.from</code>方法。
    </p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c"><span class="pl-c">//</span> 方法一</span>
<span class="pl-k">let</span> set <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">Set</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span
                class="pl-c1">3</span>]);
set <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Set</span>([<span
                class="pl-k">...</span>set].<span class="pl-en">map</span>(<span class="pl-smi">val</span> <span
                class="pl-k">=&gt;</span> val <span class="pl-k">*</span> <span class="pl-c1">2</span>));
<span class="pl-c"><span class="pl-c">//</span> set的值是2, 4, 6</span>

<span class="pl-c"><span class="pl-c">//</span> 方法二</span>
<span class="pl-k">let</span> set <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">Set</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span
                class="pl-c1">3</span>]);
set <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Set</span>(<span
                class="pl-c1">Array</span>.<span class="pl-en">from</span>(set, <span class="pl-smi">val</span> <span
                class="pl-k">=&gt;</span> val <span class="pl-k">*</span> <span class="pl-c1">2</span>));
<span class="pl-c"><span class="pl-c">//</span> set的值是2, 4, 6</span></pre>
    </div>
    <p>上面代码提供了两种方法，直接在遍历操作中改变原来的 Set 结构。</p>
    <h2><a href="#weakset"  class="anchor" id="user-content-weakset">

    </a>WeakSet
    </h2>
    <h3><a href="#含义"  class="anchor" id="user-content-含义">

    </a>含义
    </h3>
    <p>WeakSet 结构与 Set 类似，也是不重复的值的集合。但是，它与 Set 有两个区别。</p>
    <p>首先，WeakSet 的成员只能是对象，而不能是其他类型的值。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">ws</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">WeakSet</span>();
<span class="pl-smi">ws</span>.<span class="pl-c1">add</span>(<span class="pl-c1">1</span>)
<span class="pl-c"><span class="pl-c">//</span> TypeError: Invalid value used in weak set</span>
<span class="pl-smi">ws</span>.<span class="pl-c1">add</span>(<span class="pl-c1">Symbol</span>())
<span class="pl-c"><span class="pl-c">//</span> TypeError: invalid value used in weak set</span></pre>
    </div>
    <p>上面代码试图向 WeakSet 添加一个数值和<code>Symbol</code>值，结果报错，因为 WeakSet 只能放置对象。</p>
    <p>其次，WeakSet 中的对象都是弱引用，即垃圾回收机制不考虑 WeakSet 对该对象的引用，也就是说，如果其他对象都不再引用该对象，那么垃圾回收机制会自动回收该对象所占用的内存，不考虑该对象还存在于 WeakSet
        之中。</p>
    <p>这是因为垃圾回收机制依赖引用计数，如果一个值的引用次数不为<code>0</code>，垃圾回收机制就不会释放这块内存。结束使用该值之后，有时会忘记取消引用，导致内存无法释放，进而可能会引发内存泄漏。WeakSet
        里面的引用，都不计入垃圾回收机制，所以就不存在这个问题。因此，WeakSet 适合临时存放一组对象，以及存放跟对象绑定的信息。只要这些对象在外部消失，它在 WeakSet 里面的引用就会自动消失。</p>
    <p>由于上面这个特点，WeakSet 的成员是不适合引用的，因为它会随时消失。另外，由于 WeakSet 内部有多少个成员，取决于垃圾回收机制有没有运行，运行前后很可能成员个数是不一样的，而垃圾回收机制何时运行是不可预测的，因此
        ES6 规定 WeakSet 不可遍历。</p>
    <p>这些特点同样适用于本章后面要介绍的 WeakMap 结构。</p>
    <h3><a href="#语法"  class="anchor" id="user-content-语法">

    </a>语法
    </h3>
    <p>WeakSet 是一个构造函数，可以使用<code>new</code>命令，创建 WeakSet 数据结构。</p>
    <div class="highlight highlight-source-js">
        <pre><span class="pl-k">const</span> <span class="pl-c1">ws</span> <span class="pl-k">=</span> <span
                class="pl-k">new</span> <span class="pl-en">WeakSet</span>();</pre>
    </div>
    <p>作为构造函数，WeakSet 可以接受一个数组或类似数组的对象作为参数。（实际上，任何具有 Iterable 接口的对象，都可以作为 WeakSet 的参数。）该数组的所有成员，都会自动成为 WeakSet
        实例对象的成员。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">a</span> <span
            class="pl-k">=</span> [[<span class="pl-c1">1</span>, <span class="pl-c1">2</span>], [<span
            class="pl-c1">3</span>, <span class="pl-c1">4</span>]];
<span class="pl-k">const</span> <span class="pl-c1">ws</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">WeakSet</span>(a);
<span class="pl-c"><span class="pl-c">//</span> WeakSet {[1, 2], [3, 4]}</span></pre>
    </div>
    <p>上面代码中，<code>a</code>是一个数组，它有两个成员，也都是数组。将<code>a</code>作为 WeakSet 构造函数的参数，<code>a</code>的成员会自动成为 WeakSet 的成员。</p>
    <p>注意，是<code>a</code>数组的成员成为 WeakSet 的成员，而不是<code>a</code>数组本身。这意味着，数组的成员只能是对象。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">b</span> <span
            class="pl-k">=</span> [<span class="pl-c1">3</span>, <span class="pl-c1">4</span>];
<span class="pl-k">const</span> <span class="pl-c1">ws</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">WeakSet</span>(b);
<span class="pl-c"><span class="pl-c">//</span> Uncaught TypeError: Invalid value used in weak set(…)</span></pre>
    </div>
    <p>上面代码中，数组<code>b</code>的成员不是对象，加入 WeaKSet 就会报错。</p>
    <p>WeakSet 结构有以下三个方法。</p>
    <ul>
        <li><strong>WeakSet.prototype.add(value)</strong>：向 WeakSet 实例添加一个新成员。</li>
        <li><strong>WeakSet.prototype.delete(value)</strong>：清除 WeakSet 实例的指定成员。</li>
        <li><strong>WeakSet.prototype.has(value)</strong>：返回一个布尔值，表示某个值是否在 WeakSet 实例之中。</li>
    </ul>
    <p>下面是一个例子。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">ws</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">WeakSet</span>();
<span class="pl-k">const</span> <span class="pl-c1">obj</span> <span class="pl-k">=</span> {};
<span class="pl-k">const</span> <span class="pl-c1">foo</span> <span class="pl-k">=</span> {};

<span class="pl-smi">ws</span>.<span class="pl-c1">add</span>(<span class="pl-c1">window</span>);
<span class="pl-smi">ws</span>.<span class="pl-c1">add</span>(obj);

<span class="pl-smi">ws</span>.<span class="pl-c1">has</span>(<span class="pl-c1">window</span>); <span
                class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-smi">ws</span>.<span class="pl-c1">has</span>(foo);    <span class="pl-c"><span class="pl-c">//</span> false</span>

<span class="pl-smi">ws</span>.<span class="pl-c1">delete</span>(<span class="pl-c1">window</span>);
<span class="pl-smi">ws</span>.<span class="pl-c1">has</span>(<span class="pl-c1">window</span>);    <span class="pl-c"><span
                class="pl-c">//</span> false</span></pre>
    </div>
    <p>WeakSet 没有<code>size</code>属性，没有办法遍历它的成员。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-smi">ws</span>.<span class="pl-c1">size</span> <span
            class="pl-c"><span class="pl-c">//</span> undefined</span>
<span class="pl-smi">ws</span>.<span class="pl-smi">forEach</span> <span class="pl-c"><span class="pl-c">//</span> undefined</span>

<span class="pl-smi">ws</span>.<span class="pl-c1">forEach</span>(<span class="pl-k">function</span>(<span
                class="pl-smi">item</span>){ <span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span
                class="pl-s"><span class="pl-pds">'</span>WeakSet has <span class="pl-pds">'</span></span> <span
                class="pl-k">+</span> item)})
<span class="pl-c"><span class="pl-c">//</span> TypeError: undefined is not a function</span></pre>
    </div>
    <p>上面代码试图获取<code>size</code>和<code>forEach</code>属性，结果都不能成功。</p>
    <p>WeakSet 不能遍历，是因为成员都是弱引用，随时可能消失，遍历机制无法保证成员的存在，很可能刚刚遍历结束，成员就取不到了。WeakSet 的一个用处，是储存 DOM
        节点，而不用担心这些节点从文档移除时，会引发内存泄漏。</p>
    <p>下面是 WeakSet 的另一个例子。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span
            class="pl-c1">foos</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">WeakSet</span>()
<span class="pl-k">class</span> <span class="pl-en">Foo</span> {
  <span class="pl-en">constructor</span>() {
    <span class="pl-smi">foos</span>.<span class="pl-c1">add</span>(<span class="pl-c1">this</span>)
  }
  <span class="pl-en">method</span> () {
    <span class="pl-k">if</span> (<span class="pl-k">!</span><span class="pl-smi">foos</span>.<span
                class="pl-c1">has</span>(<span class="pl-c1">this</span>)) {
      <span class="pl-k">throw</span> <span class="pl-k">new</span> <span class="pl-en">TypeError</span>(<span
                class="pl-s"><span class="pl-pds">'</span>Foo.prototype.method 只能在Foo的实例上调用！<span
                class="pl-pds">'</span></span>);
    }
  }
}</pre>
    </div>
    <p>上面代码保证了<code>Foo</code>的实例方法，只能在<code>Foo</code>的实例上调用。这里使用 WeakSet 的好处是，<code>foos</code>对实例的引用，不会被计入内存回收机制，所以删除实例的时候，不用考虑<code>foos</code>，也不会出现内存泄漏。
    </p>
    <h2><a href="#map"  class="anchor" id="user-content-map">

    </a>Map
    </h2>
    <h3><a href="#含义和基本用法"  class="anchor" id="user-content-含义和基本用法">

    </a>含义和基本用法
    </h3>
    <p>JavaScript 的对象（Object），本质上是键值对的集合（Hash 结构），但是传统上只能用字符串当作键。这给它的使用带来了很大的限制。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span
            class="pl-c1">data</span> <span class="pl-k">=</span> {};
<span class="pl-k">const</span> <span class="pl-c1">element</span> <span class="pl-k">=</span> <span class="pl-c1">document</span>.<span
                class="pl-c1">getElementById</span>(<span class="pl-s"><span class="pl-pds">'</span>myDiv<span
                class="pl-pds">'</span></span>);

data[element] <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>metadata<span
                class="pl-pds">'</span></span>;
data[<span class="pl-s"><span class="pl-pds">'</span>[object HTMLDivElement]<span class="pl-pds">'</span></span>] <span
                class="pl-c"><span class="pl-c">//</span> "metadata"</span></pre>
    </div>
    <p>上面代码原意是将一个 DOM 节点作为对象<code>data</code>的键，但是由于对象只接受字符串作为键名，所以<code>element</code>被自动转为字符串<code>[object
        HTMLDivElement]</code>。</p>
    <p>为了解决这个问题，ES6 提供了 Map 数据结构。它类似于对象，也是键值对的集合，但是“键”的范围不限于字符串，各种类型的值（包括对象）都可以当作键。也就是说，Object 结构提供了“字符串—值”的对应，Map
        结构提供了“值—值”的对应，是一种更完善的 Hash 结构实现。如果你需要“键值对”的数据结构，Map 比 Object 更合适。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">m</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Map</span>();
<span class="pl-k">const</span> <span class="pl-c1">o</span> <span class="pl-k">=</span> {p<span
                class="pl-k">:</span> <span class="pl-s"><span class="pl-pds">'</span>Hello World<span
                class="pl-pds">'</span></span>};

<span class="pl-smi">m</span>.<span class="pl-c1">set</span>(o, <span class="pl-s"><span
                class="pl-pds">'</span>content<span class="pl-pds">'</span></span>)
<span class="pl-smi">m</span>.<span class="pl-c1">get</span>(o) <span class="pl-c"><span class="pl-c">//</span> "content"</span>

<span class="pl-smi">m</span>.<span class="pl-c1">has</span>(o) <span class="pl-c"><span
                class="pl-c">//</span> true</span>
<span class="pl-smi">m</span>.<span class="pl-c1">delete</span>(o) <span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-smi">m</span>.<span class="pl-c1">has</span>(o) <span class="pl-c"><span
                class="pl-c">//</span> false</span></pre>
    </div>
    <p>上面代码使用 Map 结构的<code>set</code>方法，将对象<code>o</code>当作<code>m</code>的一个键，然后又使用<code>get</code>方法读取这个键，接着使用<code>delete</code>方法删除了这个键。
    </p>
    <p>上面的例子展示了如何向 Map 添加成员。作为构造函数，Map 也可以接受一个数组作为参数。该数组的成员是一个个表示键值对的数组。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">map</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Map</span>([
  [<span class="pl-s"><span class="pl-pds">'</span>name<span class="pl-pds">'</span></span>, <span class="pl-s"><span
                class="pl-pds">'</span>张三<span class="pl-pds">'</span></span>],
  [<span class="pl-s"><span class="pl-pds">'</span>title<span class="pl-pds">'</span></span>, <span class="pl-s"><span
                class="pl-pds">'</span>Author<span class="pl-pds">'</span></span>]
]);

<span class="pl-smi">map</span>.<span class="pl-c1">size</span> <span class="pl-c"><span class="pl-c">//</span> 2</span>
<span class="pl-smi">map</span>.<span class="pl-c1">has</span>(<span class="pl-s"><span class="pl-pds">'</span>name<span
                class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-smi">map</span>.<span class="pl-c1">get</span>(<span class="pl-s"><span class="pl-pds">'</span>name<span
                class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> "张三"</span>
<span class="pl-smi">map</span>.<span class="pl-c1">has</span>(<span class="pl-s"><span
                class="pl-pds">'</span>title<span class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-smi">map</span>.<span class="pl-c1">get</span>(<span class="pl-s"><span
                class="pl-pds">'</span>title<span class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> "Author"</span></pre>
    </div>
    <p>上面代码在新建 Map 实例时，就指定了两个键<code>name</code>和<code>title</code>。</p>
    <p><code>Map</code>构造函数接受数组作为参数，实际上执行的是下面的算法。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span
            class="pl-c1">items</span> <span class="pl-k">=</span> [
  [<span class="pl-s"><span class="pl-pds">'</span>name<span class="pl-pds">'</span></span>, <span class="pl-s"><span
                class="pl-pds">'</span>张三<span class="pl-pds">'</span></span>],
  [<span class="pl-s"><span class="pl-pds">'</span>title<span class="pl-pds">'</span></span>, <span class="pl-s"><span
                class="pl-pds">'</span>Author<span class="pl-pds">'</span></span>]
];

<span class="pl-k">const</span> <span class="pl-c1">map</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">Map</span>();

<span class="pl-smi">items</span>.<span class="pl-c1">forEach</span>(
  ([<span class="pl-smi">key</span>, <span class="pl-smi">value</span>]) <span class="pl-k">=&gt;</span> <span
                class="pl-smi">map</span>.<span class="pl-c1">set</span>(key, value)
);</pre>
    </div>
    <p>事实上，不仅仅是数组，任何具有 Iterator
        接口、且每个成员都是一个双元素的数组的数据结构（详见《Iterator》一章）都可以当作<code>Map</code>构造函数的参数。这就是说，<code>Set</code>和<code>Map</code>都可以用来生成新的
        Map。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">set</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Set</span>([
  [<span class="pl-s"><span class="pl-pds">'</span>foo<span class="pl-pds">'</span></span>, <span class="pl-c1">1</span>],
  [<span class="pl-s"><span class="pl-pds">'</span>bar<span class="pl-pds">'</span></span>, <span class="pl-c1">2</span>]
]);
<span class="pl-k">const</span> <span class="pl-c1">m1</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">Map</span>(set);
<span class="pl-smi">m1</span>.<span class="pl-c1">get</span>(<span class="pl-s"><span class="pl-pds">'</span>foo<span
                class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> 1</span>

<span class="pl-k">const</span> <span class="pl-c1">m2</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">Map</span>([[<span class="pl-s"><span class="pl-pds">'</span>baz<span
                class="pl-pds">'</span></span>, <span class="pl-c1">3</span>]]);
<span class="pl-k">const</span> <span class="pl-c1">m3</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">Map</span>(m2);
<span class="pl-smi">m3</span>.<span class="pl-c1">get</span>(<span class="pl-s"><span class="pl-pds">'</span>baz<span
                class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> 3</span></pre>
    </div>
    <p>上面代码中，我们分别使用 Set 对象和 Map 对象，当作<code>Map</code>构造函数的参数，结果都生成了新的 Map 对象。</p>
    <p>如果对同一个键多次赋值，后面的值将覆盖前面的值。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">map</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Map</span>();

map
.<span class="pl-c1">set</span>(<span class="pl-c1">1</span>, <span class="pl-s"><span class="pl-pds">'</span>aaa<span
                class="pl-pds">'</span></span>)
.<span class="pl-c1">set</span>(<span class="pl-c1">1</span>, <span class="pl-s"><span class="pl-pds">'</span>bbb<span
                class="pl-pds">'</span></span>);

<span class="pl-smi">map</span>.<span class="pl-c1">get</span>(<span class="pl-c1">1</span>) <span class="pl-c"><span
                class="pl-c">//</span> "bbb"</span></pre>
    </div>
    <p>上面代码对键<code>1</code>连续赋值两次，后一次的值覆盖前一次的值。</p>
    <p>如果读取一个未知的键，则返回<code>undefined</code>。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">new</span> <span class="pl-en">Map</span>().<span
            class="pl-c1">get</span>(<span class="pl-s"><span class="pl-pds">'</span>asfddfsasadf<span
            class="pl-pds">'</span></span>)
<span class="pl-c"><span class="pl-c">//</span> undefined</span></pre>
    </div>
    <p>注意，只有对同一个对象的引用，Map 结构才将其视为同一个键。这一点要非常小心。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">map</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Map</span>();

<span class="pl-smi">map</span>.<span class="pl-c1">set</span>([<span class="pl-s"><span class="pl-pds">'</span>a<span
                class="pl-pds">'</span></span>], <span class="pl-c1">555</span>);
<span class="pl-smi">map</span>.<span class="pl-c1">get</span>([<span class="pl-s"><span class="pl-pds">'</span>a<span
                class="pl-pds">'</span></span>]) <span class="pl-c"><span class="pl-c">//</span> undefined</span></pre>
    </div>
    <p>上面代码的<code>set</code>和<code>get</code>方法，表面是针对同一个键，但实际上这是两个值，内存地址是不一样的，因此<code>get</code>方法无法读取该键，返回<code>undefined</code>。
    </p>
    <p>同理，同样的值的两个实例，在 Map 结构中被视为两个键。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">map</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Map</span>();

<span class="pl-k">const</span> <span class="pl-c1">k1</span> <span class="pl-k">=</span> [<span class="pl-s"><span
                class="pl-pds">'</span>a<span class="pl-pds">'</span></span>];
<span class="pl-k">const</span> <span class="pl-c1">k2</span> <span class="pl-k">=</span> [<span class="pl-s"><span
                class="pl-pds">'</span>a<span class="pl-pds">'</span></span>];

map
.<span class="pl-c1">set</span>(k1, <span class="pl-c1">111</span>)
.<span class="pl-c1">set</span>(k2, <span class="pl-c1">222</span>);

<span class="pl-smi">map</span>.<span class="pl-c1">get</span>(k1) <span class="pl-c"><span
                class="pl-c">//</span> 111</span>
<span class="pl-smi">map</span>.<span class="pl-c1">get</span>(k2) <span class="pl-c"><span
                class="pl-c">//</span> 222</span></pre>
    </div>
    <p>上面代码中，变量<code>k1</code>和<code>k2</code>的值是一样的，但是它们在 Map 结构中被视为两个键。</p>
    <p>由上可知，Map 的键实际上是跟内存地址绑定的，只要内存地址不一样，就视为两个键。这就解决了同名属性碰撞（clash）的问题，我们扩展别人的库的时候，如果使用对象作为键名，就不用担心自己的属性与原作者的属性同名。</p>
    <p>如果 Map 的键是一个简单类型的值（数字、字符串、布尔值），则只要两个值严格相等，Map 将其视为一个键，比如<code>0</code>和<code>-0</code>就是一个键，布尔值<code>true</code>和字符串<code>true</code>则是两个不同的键。另外，<code>undefined</code>和<code>null</code>也是两个不同的键。虽然<code>NaN</code>不严格相等于自身，但
        Map 将其视为同一个键。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">let</span> map <span class="pl-k">=</span> <span
            class="pl-k">new</span> <span class="pl-en">Map</span>();

<span class="pl-smi">map</span>.<span class="pl-c1">set</span>(<span class="pl-k">-</span><span class="pl-c1">0</span>, <span
                class="pl-c1">123</span>);
<span class="pl-smi">map</span>.<span class="pl-c1">get</span>(<span class="pl-k">+</span><span class="pl-c1">0</span>) <span
                class="pl-c"><span class="pl-c">//</span> 123</span>

<span class="pl-smi">map</span>.<span class="pl-c1">set</span>(<span class="pl-c1">true</span>, <span
                class="pl-c1">1</span>);
<span class="pl-smi">map</span>.<span class="pl-c1">set</span>(<span class="pl-s"><span class="pl-pds">'</span>true<span
                class="pl-pds">'</span></span>, <span class="pl-c1">2</span>);
<span class="pl-smi">map</span>.<span class="pl-c1">get</span>(<span class="pl-c1">true</span>) <span class="pl-c"><span
                class="pl-c">//</span> 1</span>

<span class="pl-smi">map</span>.<span class="pl-c1">set</span>(<span class="pl-c1">undefined</span>, <span
                class="pl-c1">3</span>);
<span class="pl-smi">map</span>.<span class="pl-c1">set</span>(<span class="pl-c1">null</span>, <span
                class="pl-c1">4</span>);
<span class="pl-smi">map</span>.<span class="pl-c1">get</span>(<span class="pl-c1">undefined</span>) <span class="pl-c"><span
                class="pl-c">//</span> 3</span>

<span class="pl-smi">map</span>.<span class="pl-c1">set</span>(<span class="pl-c1">NaN</span>, <span
                class="pl-c1">123</span>);
<span class="pl-smi">map</span>.<span class="pl-c1">get</span>(<span class="pl-c1">NaN</span>) <span class="pl-c"><span
                class="pl-c">//</span> 123</span></pre>
    </div>
    <h3><a href="#实例的属性和操作方法"  class="anchor" id="user-content-实例的属性和操作方法">

    </a>实例的属性和操作方法
    </h3>
    <p>Map 结构的实例有以下属性和操作方法。</p>
    <p><strong>（1）size 属性</strong></p>
    <p><code>size</code>属性返回 Map 结构的成员总数。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">map</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Map</span>();
<span class="pl-smi">map</span>.<span class="pl-c1">set</span>(<span class="pl-s"><span class="pl-pds">'</span>foo<span
                class="pl-pds">'</span></span>, <span class="pl-c1">true</span>);
<span class="pl-smi">map</span>.<span class="pl-c1">set</span>(<span class="pl-s"><span class="pl-pds">'</span>bar<span
                class="pl-pds">'</span></span>, <span class="pl-c1">false</span>);

<span class="pl-smi">map</span>.<span class="pl-c1">size</span> <span class="pl-c"><span class="pl-c">//</span> 2</span></pre>
    </div>
    <p><strong>（2）set(key, value)</strong></p>
    <p><code>set</code>方法设置键名<code>key</code>对应的键值为<code>value</code>，然后返回整个 Map 结构。如果<code>key</code>已经有值，则键值会被更新，否则就新生成该键。
    </p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">m</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Map</span>();

<span class="pl-smi">m</span>.<span class="pl-c1">set</span>(<span class="pl-s"><span
                class="pl-pds">'</span>edition<span class="pl-pds">'</span></span>, <span class="pl-c1">6</span>)        <span
                class="pl-c"><span class="pl-c">//</span> 键是字符串</span>
<span class="pl-smi">m</span>.<span class="pl-c1">set</span>(<span class="pl-c1">262</span>, <span class="pl-s"><span
                class="pl-pds">'</span>standard<span class="pl-pds">'</span></span>)     <span class="pl-c"><span
                class="pl-c">//</span> 键是数值</span>
<span class="pl-smi">m</span>.<span class="pl-c1">set</span>(<span class="pl-c1">undefined</span>, <span
                class="pl-s"><span class="pl-pds">'</span>nah<span class="pl-pds">'</span></span>)    <span
                class="pl-c"><span class="pl-c">//</span> 键是 undefined</span></pre>
    </div>
    <p><code>set</code>方法返回的是当前的<code>Map</code>对象，因此可以采用链式写法。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">let</span> map <span class="pl-k">=</span> <span
            class="pl-k">new</span> <span class="pl-en">Map</span>()
  .<span class="pl-c1">set</span>(<span class="pl-c1">1</span>, <span class="pl-s"><span class="pl-pds">'</span>a<span
                class="pl-pds">'</span></span>)
  .<span class="pl-c1">set</span>(<span class="pl-c1">2</span>, <span class="pl-s"><span class="pl-pds">'</span>b<span
                class="pl-pds">'</span></span>)
  .<span class="pl-c1">set</span>(<span class="pl-c1">3</span>, <span class="pl-s"><span class="pl-pds">'</span>c<span
                class="pl-pds">'</span></span>);</pre>
    </div>
    <p><strong>（3）get(key)</strong></p>
    <p><code>get</code>方法读取<code>key</code>对应的键值，如果找不到<code>key</code>，返回<code>undefined</code>。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">m</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Map</span>();

<span class="pl-k">const</span> <span class="pl-c1">hello</span> <span class="pl-k">=</span> <span
                class="pl-k">function</span>() {<span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span
                class="pl-s"><span class="pl-pds">'</span>hello<span class="pl-pds">'</span></span>);};
<span class="pl-smi">m</span>.<span class="pl-c1">set</span>(hello, <span class="pl-s"><span class="pl-pds">'</span>Hello ES6!<span
                class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> 键是函数</span>

<span class="pl-smi">m</span>.<span class="pl-c1">get</span>(hello)  <span class="pl-c"><span class="pl-c">//</span> Hello ES6!</span></pre>
    </div>
    <p><strong>（4）has(key)</strong></p>
    <p><code>has</code>方法返回一个布尔值，表示某个键是否在当前 Map 对象之中。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">m</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Map</span>();

<span class="pl-smi">m</span>.<span class="pl-c1">set</span>(<span class="pl-s"><span
                class="pl-pds">'</span>edition<span class="pl-pds">'</span></span>, <span class="pl-c1">6</span>);
<span class="pl-smi">m</span>.<span class="pl-c1">set</span>(<span class="pl-c1">262</span>, <span class="pl-s"><span
                class="pl-pds">'</span>standard<span class="pl-pds">'</span></span>);
<span class="pl-smi">m</span>.<span class="pl-c1">set</span>(<span class="pl-c1">undefined</span>, <span
                class="pl-s"><span class="pl-pds">'</span>nah<span class="pl-pds">'</span></span>);

<span class="pl-smi">m</span>.<span class="pl-c1">has</span>(<span class="pl-s"><span
                class="pl-pds">'</span>edition<span class="pl-pds">'</span></span>)     <span class="pl-c"><span
                class="pl-c">//</span> true</span>
<span class="pl-smi">m</span>.<span class="pl-c1">has</span>(<span class="pl-s"><span class="pl-pds">'</span>years<span
                class="pl-pds">'</span></span>)       <span class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-smi">m</span>.<span class="pl-c1">has</span>(<span class="pl-c1">262</span>)           <span
                class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-smi">m</span>.<span class="pl-c1">has</span>(<span class="pl-c1">undefined</span>)     <span
                class="pl-c"><span class="pl-c">//</span> true</span></pre>
    </div>
    <p><strong>（5）delete(key)</strong></p>
    <p><code>delete</code>方法删除某个键，返回<code>true</code>。如果删除失败，返回<code>false</code>。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">m</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Map</span>();
<span class="pl-smi">m</span>.<span class="pl-c1">set</span>(<span class="pl-c1">undefined</span>, <span
                class="pl-s"><span class="pl-pds">'</span>nah<span class="pl-pds">'</span></span>);
<span class="pl-smi">m</span>.<span class="pl-c1">has</span>(<span class="pl-c1">undefined</span>)     <span
                class="pl-c"><span class="pl-c">//</span> true</span>

<span class="pl-smi">m</span>.<span class="pl-c1">delete</span>(<span class="pl-c1">undefined</span>)
<span class="pl-smi">m</span>.<span class="pl-c1">has</span>(<span class="pl-c1">undefined</span>)       <span
                class="pl-c"><span class="pl-c">//</span> false</span></pre>
    </div>
    <p><strong>（6）clear()</strong></p>
    <p><code>clear</code>方法清除所有成员，没有返回值。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">let</span> map <span class="pl-k">=</span> <span
            class="pl-k">new</span> <span class="pl-en">Map</span>();
<span class="pl-smi">map</span>.<span class="pl-c1">set</span>(<span class="pl-s"><span class="pl-pds">'</span>foo<span
                class="pl-pds">'</span></span>, <span class="pl-c1">true</span>);
<span class="pl-smi">map</span>.<span class="pl-c1">set</span>(<span class="pl-s"><span class="pl-pds">'</span>bar<span
                class="pl-pds">'</span></span>, <span class="pl-c1">false</span>);

<span class="pl-smi">map</span>.<span class="pl-c1">size</span> <span class="pl-c"><span class="pl-c">//</span> 2</span>
<span class="pl-smi">map</span>.<span class="pl-c1">clear</span>()
<span class="pl-smi">map</span>.<span class="pl-c1">size</span> <span class="pl-c"><span class="pl-c">//</span> 0</span></pre>
    </div>
    <h3><a href="#遍历方法"  class="anchor" id="user-content-遍历方法">

    </a>遍历方法
    </h3>
    <p>Map 结构原生提供三个遍历器生成函数和一个遍历方法。</p>
    <ul>
        <li><code>keys()</code>：返回键名的遍历器。</li>
        <li><code>values()</code>：返回键值的遍历器。</li>
        <li><code>entries()</code>：返回所有成员的遍历器。</li>
        <li><code>forEach()</code>：遍历 Map 的所有成员。</li>
    </ul>
    <p>需要特别注意的是，Map 的遍历顺序就是插入顺序。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">map</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Map</span>([
  [<span class="pl-s"><span class="pl-pds">'</span>F<span class="pl-pds">'</span></span>, <span class="pl-s"><span
                class="pl-pds">'</span>no<span class="pl-pds">'</span></span>],
  [<span class="pl-s"><span class="pl-pds">'</span>T<span class="pl-pds">'</span></span>,  <span class="pl-s"><span
                class="pl-pds">'</span>yes<span class="pl-pds">'</span></span>],
]);

<span class="pl-k">for</span> (<span class="pl-k">let</span> key <span class="pl-k">of</span> <span
                class="pl-smi">map</span>.<span class="pl-c1">keys</span>()) {
  <span class="pl-en">console</span>.<span class="pl-c1">log</span>(key);
}
<span class="pl-c"><span class="pl-c">//</span> "F"</span>
<span class="pl-c"><span class="pl-c">//</span> "T"</span>

<span class="pl-k">for</span> (<span class="pl-k">let</span> value <span class="pl-k">of</span> <span
                class="pl-smi">map</span>.<span class="pl-c1">values</span>()) {
  <span class="pl-en">console</span>.<span class="pl-c1">log</span>(value);
}
<span class="pl-c"><span class="pl-c">//</span> "no"</span>
<span class="pl-c"><span class="pl-c">//</span> "yes"</span>

<span class="pl-k">for</span> (<span class="pl-k">let</span> item <span class="pl-k">of</span> <span
                class="pl-smi">map</span>.<span class="pl-c1">entries</span>()) {
  <span class="pl-en">console</span>.<span class="pl-c1">log</span>(item[<span class="pl-c1">0</span>], item[<span
                class="pl-c1">1</span>]);
}
<span class="pl-c"><span class="pl-c">//</span> "F" "no"</span>
<span class="pl-c"><span class="pl-c">//</span> "T" "yes"</span>

<span class="pl-c"><span class="pl-c">//</span> 或者</span>
<span class="pl-k">for</span> (<span class="pl-k">let</span> [key, value] <span class="pl-k">of</span> <span
                class="pl-smi">map</span>.<span class="pl-c1">entries</span>()) {
  <span class="pl-en">console</span>.<span class="pl-c1">log</span>(key, value);
}
<span class="pl-c"><span class="pl-c">//</span> "F" "no"</span>
<span class="pl-c"><span class="pl-c">//</span> "T" "yes"</span>

<span class="pl-c"><span class="pl-c">//</span> 等同于使用map.entries()</span>
<span class="pl-k">for</span> (<span class="pl-k">let</span> [key, value] <span class="pl-k">of</span> map) {
  <span class="pl-en">console</span>.<span class="pl-c1">log</span>(key, value);
}
<span class="pl-c"><span class="pl-c">//</span> "F" "no"</span>
<span class="pl-c"><span class="pl-c">//</span> "T" "yes"</span></pre>
    </div>
    <p>上面代码最后的那个例子，表示 Map 结构的默认遍历器接口（<code>Symbol.iterator</code>属性），就是<code>entries</code>方法。</p>
    <div class="highlight highlight-source-js"><pre>map[<span class="pl-c1">Symbol</span>.<span
            class="pl-smi">iterator</span>] <span class="pl-k">===</span> <span class="pl-smi">map</span>.<span
            class="pl-smi">entries</span>
<span class="pl-c"><span class="pl-c">//</span> true</span></pre>
    </div>
    <p>Map 结构转为数组结构，比较快速的方法是使用扩展运算符（<code>...</code>）。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">map</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">Map</span>([
  [<span class="pl-c1">1</span>, <span class="pl-s"><span class="pl-pds">'</span>one<span class="pl-pds">'</span></span>],
  [<span class="pl-c1">2</span>, <span class="pl-s"><span class="pl-pds">'</span>two<span class="pl-pds">'</span></span>],
  [<span class="pl-c1">3</span>, <span class="pl-s"><span class="pl-pds">'</span>three<span
                class="pl-pds">'</span></span>],
]);

[<span class="pl-k">...</span><span class="pl-smi">map</span>.<span class="pl-c1">keys</span>()]
<span class="pl-c"><span class="pl-c">//</span> [1, 2, 3]</span>

[<span class="pl-k">...</span><span class="pl-smi">map</span>.<span class="pl-c1">values</span>()]
<span class="pl-c"><span class="pl-c">//</span> ['one', 'two', 'three']</span>

[<span class="pl-k">...</span><span class="pl-smi">map</span>.<span class="pl-c1">entries</span>()]
<span class="pl-c"><span class="pl-c">//</span> [[1,'one'], [2, 'two'], [3, 'three']]</span>

[<span class="pl-k">...</span>map]
<span class="pl-c"><span class="pl-c">//</span> [[1,'one'], [2, 'two'], [3, 'three']]</span></pre>
    </div>
    <p>结合数组的<code>map</code>方法、<code>filter</code>方法，可以实现 Map 的遍历和过滤（Map 本身没有<code>map</code>和<code>filter</code>方法）。
    </p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span
            class="pl-c1">map0</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span
            class="pl-en">Map</span>()
  .<span class="pl-c1">set</span>(<span class="pl-c1">1</span>, <span class="pl-s"><span class="pl-pds">'</span>a<span
                class="pl-pds">'</span></span>)
  .<span class="pl-c1">set</span>(<span class="pl-c1">2</span>, <span class="pl-s"><span class="pl-pds">'</span>b<span
                class="pl-pds">'</span></span>)
  .<span class="pl-c1">set</span>(<span class="pl-c1">3</span>, <span class="pl-s"><span class="pl-pds">'</span>c<span
                class="pl-pds">'</span></span>);

<span class="pl-k">const</span> <span class="pl-c1">map1</span> <span class="pl-k">=</span> <span
                class="pl-k">new</span> <span class="pl-en">Map</span>(
  [<span class="pl-k">...</span>map0].<span class="pl-en">filter</span>(([<span class="pl-smi">k</span>, <span
                class="pl-smi">v</span>]) <span class="pl-k">=&gt;</span> k <span class="pl-k">&lt;</span> <span
                class="pl-c1">3</span>)
);
<span class="pl-c"><span class="pl-c">//</span> 产生 Map 结构 {1 =&gt; 'a', 2 =&gt; 'b'}</span>

<span class="pl-k">const</span> <span class="pl-c1">map2</span> <span class="pl-k">=</span> <span
                class="pl-k">new</span> <span class="pl-en">Map</span>(
  [<span class="pl-k">...</span>map0].<span class="pl-en">map</span>(([<span class="pl-smi">k</span>, <span
                class="pl-smi">v</span>]) <span class="pl-k">=&gt;</span> [k <span class="pl-k">*</span> <span
                class="pl-c1">2</span>, <span class="pl-s"><span class="pl-pds">'</span>_<span
                class="pl-pds">'</span></span> <span class="pl-k">+</span> v])
    );
<span class="pl-c"><span class="pl-c">//</span> 产生 Map 结构 {2 =&gt; '_a', 4 =&gt; '_b', 6 =&gt; '_c'}</span></pre>
    </div>
    <p>此外，Map 还有一个<code>forEach</code>方法，与数组的<code>forEach</code>方法类似，也可以实现遍历。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-smi">map</span>.<span
            class="pl-c1">forEach</span>(<span class="pl-k">function</span>(<span class="pl-smi">value</span>, <span
            class="pl-smi">key</span>, <span class="pl-smi">map</span>) {
  <span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">"</span>Key: %s, Value: %s<span
                class="pl-pds">"</span></span>, key, value);
});</pre>
    </div>
    <p><code>forEach</code>方法还可以接受第二个参数，用来绑定<code>this</code>。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">reporter</span> <span
            class="pl-k">=</span> {
  <span class="pl-en">report</span><span class="pl-k">:</span> <span class="pl-k">function</span>(<span class="pl-smi">key</span>, <span
                class="pl-smi">value</span>) {
    <span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">"</span>Key: %s, Value: %s<span
                class="pl-pds">"</span></span>, key, value);
  }
};

<span class="pl-smi">map</span>.<span class="pl-c1">forEach</span>(<span class="pl-k">function</span>(<span
                class="pl-smi">value</span>, <span class="pl-smi">key</span>, <span class="pl-smi">map</span>) {
  <span class="pl-c1">this</span>.<span class="pl-en">report</span>(key, value);
}, reporter);</pre>
    </div>
    <p>上面代码中，<code>forEach</code>方法的回调函数的<code>this</code>，就指向<code>reporter</code>。</p>
    <h3><a href="#与其他数据结构的互相转换"  class="anchor" id="user-content-与其他数据结构的互相转换">

    </a>与其他数据结构的互相转换
    </h3>
    <p><strong>（1）Map 转为数组</strong></p>
    <p>前面已经提过，Map 转为数组最方便的方法，就是使用扩展运算符（<code>...</code>）。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span
            class="pl-c1">myMap</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span
            class="pl-en">Map</span>()
  .<span class="pl-c1">set</span>(<span class="pl-c1">true</span>, <span class="pl-c1">7</span>)
  .<span class="pl-c1">set</span>({foo<span class="pl-k">:</span> <span class="pl-c1">3</span>}, [<span
                class="pl-s"><span class="pl-pds">'</span>abc<span class="pl-pds">'</span></span>]);
[<span class="pl-k">...</span>myMap]
<span class="pl-c"><span class="pl-c">//</span> [ [ true, 7 ], [ { foo: 3 }, [ 'abc' ] ] ]</span></pre>
    </div>
    <p><strong>（2）数组 转为 Map</strong></p>
    <p>将数组传入 Map 构造函数，就可以转为 Map。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">new</span> <span class="pl-en">Map</span>([
  [<span class="pl-c1">true</span>, <span class="pl-c1">7</span>],
  [{foo<span class="pl-k">:</span> <span class="pl-c1">3</span>}, [<span class="pl-s"><span
                class="pl-pds">'</span>abc<span class="pl-pds">'</span></span>]]
])
<span class="pl-c"><span class="pl-c">//</span> Map {</span>
<span class="pl-c"><span class="pl-c">//</span>   true =&gt; 7,</span>
<span class="pl-c"><span class="pl-c">//</span>   Object {foo: 3} =&gt; ['abc']</span>
<span class="pl-c"><span class="pl-c">//</span> }</span></pre>
    </div>
    <p><strong>（3）Map 转为对象</strong></p>
    <p>如果所有 Map 的键都是字符串，它可以转为对象。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">function</span> <span
            class="pl-en">strMapToObj</span>(<span class="pl-smi">strMap</span>) {
  <span class="pl-k">let</span> obj <span class="pl-k">=</span> <span class="pl-c1">Object</span>.<span class="pl-en">create</span>(<span
                class="pl-c1">null</span>);
  <span class="pl-k">for</span> (<span class="pl-k">let</span> [k,v] <span class="pl-k">of</span> strMap) {
    obj[k] <span class="pl-k">=</span> v;
  }
  <span class="pl-k">return</span> obj;
}

<span class="pl-k">const</span> <span class="pl-c1">myMap</span> <span class="pl-k">=</span> <span
                class="pl-k">new</span> <span class="pl-en">Map</span>()
  .<span class="pl-c1">set</span>(<span class="pl-s"><span class="pl-pds">'</span>yes<span
                class="pl-pds">'</span></span>, <span class="pl-c1">true</span>)
  .<span class="pl-c1">set</span>(<span class="pl-s"><span class="pl-pds">'</span>no<span class="pl-pds">'</span></span>, <span
                class="pl-c1">false</span>);
<span class="pl-en">strMapToObj</span>(myMap)
<span class="pl-c"><span class="pl-c">//</span> { yes: true, no: false }</span></pre>
    </div>
    <p><strong>（4）对象转为 Map</strong></p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">function</span> <span
            class="pl-en">objToStrMap</span>(<span class="pl-smi">obj</span>) {
  <span class="pl-k">let</span> strMap <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">Map</span>();
  <span class="pl-k">for</span> (<span class="pl-k">let</span> k <span class="pl-k">of</span> <span
                class="pl-c1">Object</span>.<span class="pl-c1">keys</span>(obj)) {
    <span class="pl-smi">strMap</span>.<span class="pl-c1">set</span>(k, obj[k]);
  }
  <span class="pl-k">return</span> strMap;
}

<span class="pl-en">objToStrMap</span>({yes<span class="pl-k">:</span> <span class="pl-c1">true</span>, no<span
                class="pl-k">:</span> <span class="pl-c1">false</span>})
<span class="pl-c"><span class="pl-c">//</span> Map {"yes" =&gt; true, "no" =&gt; false}</span></pre>
    </div>
    <p><strong>（5）Map 转为 JSON</strong></p>
    <p>Map 转为 JSON 要区分两种情况。一种情况是，Map 的键名都是字符串，这时可以选择转为对象 JSON。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">function</span> <span
            class="pl-en">strMapToJson</span>(<span class="pl-smi">strMap</span>) {
  <span class="pl-k">return</span> <span class="pl-c1">JSON</span>.<span class="pl-c1">stringify</span>(<span
                class="pl-en">strMapToObj</span>(strMap));
}

<span class="pl-k">let</span> myMap <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">Map</span>().<span class="pl-c1">set</span>(<span class="pl-s"><span
                class="pl-pds">'</span>yes<span class="pl-pds">'</span></span>, <span class="pl-c1">true</span>).<span
                class="pl-c1">set</span>(<span class="pl-s"><span class="pl-pds">'</span>no<span class="pl-pds">'</span></span>, <span
                class="pl-c1">false</span>);
<span class="pl-en">strMapToJson</span>(myMap)
<span class="pl-c"><span class="pl-c">//</span> '{"yes":true,"no":false}'</span></pre>
    </div>
    <p>另一种情况是，Map 的键名有非字符串，这时可以选择转为数组 JSON。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">function</span> <span class="pl-en">mapToArrayJson</span>(<span
            class="pl-smi">map</span>) {
  <span class="pl-k">return</span> <span class="pl-c1">JSON</span>.<span class="pl-c1">stringify</span>([<span
                class="pl-k">...</span>map]);
}

<span class="pl-k">let</span> myMap <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">Map</span>().<span class="pl-c1">set</span>(<span class="pl-c1">true</span>, <span
                class="pl-c1">7</span>).<span class="pl-c1">set</span>({foo<span class="pl-k">:</span> <span
                class="pl-c1">3</span>}, [<span class="pl-s"><span class="pl-pds">'</span>abc<span
                class="pl-pds">'</span></span>]);
<span class="pl-en">mapToArrayJson</span>(myMap)
<span class="pl-c"><span class="pl-c">//</span> '[[true,7],[{"foo":3},["abc"]]]'</span></pre>
    </div>
    <p><strong>（6）JSON 转为 Map</strong></p>
    <p>JSON 转为 Map，正常情况下，所有键名都是字符串。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">function</span> <span
            class="pl-en">jsonToStrMap</span>(<span class="pl-smi">jsonStr</span>) {
  <span class="pl-k">return</span> <span class="pl-en">objToStrMap</span>(<span class="pl-c1">JSON</span>.<span
                class="pl-c1">parse</span>(jsonStr));
}

<span class="pl-en">jsonToStrMap</span>(<span class="pl-s"><span class="pl-pds">'</span>{"yes": true, "no": false}<span
                class="pl-pds">'</span></span>)
<span class="pl-c"><span class="pl-c">//</span> Map {'yes' =&gt; true, 'no' =&gt; false}</span></pre>
    </div>
    <p>但是，有一种特殊情况，整个 JSON 就是一个数组，且每个数组成员本身，又是一个有两个成员的数组。这时，它可以一一对应地转为 Map。这往往是数组转为 JSON 的逆操作。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">function</span> <span
            class="pl-en">jsonToMap</span>(<span class="pl-smi">jsonStr</span>) {
  <span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-en">Map</span>(<span
                class="pl-c1">JSON</span>.<span class="pl-c1">parse</span>(jsonStr));
}

<span class="pl-en">jsonToMap</span>(<span class="pl-s"><span class="pl-pds">'</span>[[true,7],[{"foo":3},["abc"]]]<span
                class="pl-pds">'</span></span>)
<span class="pl-c"><span class="pl-c">//</span> Map {true =&gt; 7, Object {foo: 3} =&gt; ['abc']}</span></pre>
    </div>
    <h2><a href="#weakmap"  class="anchor" id="user-content-weakmap">

    </a>WeakMap
    </h2>
    <h3><a href="#含义-1"  class="anchor" id="user-content-含义-1">

    </a>含义
    </h3>
    <p><code>WeakMap</code>结构与<code>Map</code>结构类似，也是用于生成键值对的集合。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c"><span class="pl-c">//</span> WeakMap 可以使用 set 方法添加成员</span>
<span class="pl-k">const</span> <span class="pl-c1">wm1</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">WeakMap</span>();
<span class="pl-k">const</span> <span class="pl-c1">key</span> <span class="pl-k">=</span> {foo<span
                class="pl-k">:</span> <span class="pl-c1">1</span>};
<span class="pl-smi">wm1</span>.<span class="pl-c1">set</span>(key, <span class="pl-c1">2</span>);
<span class="pl-smi">wm1</span>.<span class="pl-c1">get</span>(key) <span class="pl-c"><span
                class="pl-c">//</span> 2</span>

<span class="pl-c"><span class="pl-c">//</span> WeakMap 也可以接受一个数组，</span>
<span class="pl-c"><span class="pl-c">//</span> 作为构造函数的参数</span>
<span class="pl-k">const</span> <span class="pl-c1">k1</span> <span class="pl-k">=</span> [<span class="pl-c1">1</span>, <span
                class="pl-c1">2</span>, <span class="pl-c1">3</span>];
<span class="pl-k">const</span> <span class="pl-c1">k2</span> <span class="pl-k">=</span> [<span class="pl-c1">4</span>, <span
                class="pl-c1">5</span>, <span class="pl-c1">6</span>];
<span class="pl-k">const</span> <span class="pl-c1">wm2</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span
                class="pl-en">WeakMap</span>([[k1, <span class="pl-s"><span class="pl-pds">'</span>foo<span
                class="pl-pds">'</span></span>], [k2, <span class="pl-s"><span class="pl-pds">'</span>bar<span
                class="pl-pds">'</span></span>]]);
<span class="pl-smi">wm2</span>.<span class="pl-c1">get</span>(k2) <span class="pl-c"><span class="pl-c">//</span> "bar"</span></pre>
    </div>
    <p><code>WeakMap</code>与<code>Map</code>的区别有两点。</p>
    <p>首先，<code>WeakMap</code>只接受对象作为键名（<code>null</code>除外），不接受其他类型的值作为键名。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">map</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">WeakMap</span>();
<span class="pl-smi">map</span>.<span class="pl-c1">set</span>(<span class="pl-c1">1</span>, <span
                class="pl-c1">2</span>)
<span class="pl-c"><span class="pl-c">//</span> TypeError: 1 is not an object!</span>
<span class="pl-smi">map</span>.<span class="pl-c1">set</span>(<span class="pl-c1">Symbol</span>(), <span class="pl-c1">2</span>)
<span class="pl-c"><span class="pl-c">//</span> TypeError: Invalid value used as weak map key</span>
<span class="pl-smi">map</span>.<span class="pl-c1">set</span>(<span class="pl-c1">null</span>, <span
                class="pl-c1">2</span>)
<span class="pl-c"><span class="pl-c">//</span> TypeError: Invalid value used as weak map key</span></pre>
    </div>
    <p>上面代码中，如果将数值<code>1</code>和<code>Symbol</code>值作为 WeakMap 的键名，都会报错。</p>
    <p>其次，<code>WeakMap</code>的键名所指向的对象，不计入垃圾回收机制。</p>
    <p><code>WeakMap</code>的设计目的在于，有时我们想在某个对象上面存放一些数据，但是这会形成对于这个对象的引用。请看下面的例子。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">e1</span> <span
            class="pl-k">=</span> <span class="pl-c1">document</span>.<span class="pl-c1">getElementById</span>(<span
            class="pl-s"><span class="pl-pds">'</span>foo<span class="pl-pds">'</span></span>);
<span class="pl-k">const</span> <span class="pl-c1">e2</span> <span class="pl-k">=</span> <span
                class="pl-c1">document</span>.<span class="pl-c1">getElementById</span>(<span class="pl-s"><span
                class="pl-pds">'</span>bar<span class="pl-pds">'</span></span>);
<span class="pl-k">const</span> <span class="pl-c1">arr</span> <span class="pl-k">=</span> [
  [e1, <span class="pl-s"><span class="pl-pds">'</span>foo 元素<span class="pl-pds">'</span></span>],
  [e2, <span class="pl-s"><span class="pl-pds">'</span>bar 元素<span class="pl-pds">'</span></span>],
];</pre>
    </div>
    <p>
        上面代码中，<code>e1</code>和<code>e2</code>是两个对象，我们通过<code>arr</code>数组对这两个对象添加一些文字说明。这就形成了<code>arr</code>对<code>e1</code>和<code>e2</code>的引用。
    </p>
    <p>一旦不再需要这两个对象，我们就必须手动删除这个引用，否则垃圾回收机制就不会释放<code>e1</code>和<code>e2</code>占用的内存。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c"><span
            class="pl-c">//</span> 不需要 e1 和 e2 的时候</span>
<span class="pl-c"><span class="pl-c">//</span> 必须手动删除引用</span>
arr [<span class="pl-c1">0</span>] <span class="pl-k">=</span> <span class="pl-c1">null</span>;
arr [<span class="pl-c1">1</span>] <span class="pl-k">=</span> <span class="pl-c1">null</span>;</pre>
    </div>
    <p>上面这样的写法显然很不方便。一旦忘了写，就会造成内存泄露。</p>
    <p>WeakMap
        就是为了解决这个问题而诞生的，它的键名所引用的对象都是弱引用，即垃圾回收机制不将该引用考虑在内。因此，只要所引用的对象的其他引用都被清除，垃圾回收机制就会释放该对象所占用的内存。也就是说，一旦不再需要，WeakMap
        里面的键名对象和所对应的键值对会自动消失，不用手动删除引用。</p>
    <p>基本上，如果你要往对象上添加数据，又不想干扰垃圾回收机制，就可以使用 WeakMap。一个典型应用场景是，在网页的 DOM 元素上添加数据，就可以使用<code>WeakMap</code>结构。当该 DOM
        元素被清除，其所对应的<code>WeakMap</code>记录就会自动被移除。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">wm</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">WeakMap</span>();

<span class="pl-k">const</span> <span class="pl-c1">element</span> <span class="pl-k">=</span> <span class="pl-c1">document</span>.<span
                class="pl-c1">getElementById</span>(<span class="pl-s"><span class="pl-pds">'</span>example<span
                class="pl-pds">'</span></span>);

<span class="pl-smi">wm</span>.<span class="pl-c1">set</span>(element, <span class="pl-s"><span class="pl-pds">'</span>some information<span
                class="pl-pds">'</span></span>);
<span class="pl-smi">wm</span>.<span class="pl-c1">get</span>(element) <span class="pl-c"><span class="pl-c">//</span> "some information"</span></pre>
    </div>
    <p>上面代码中，先新建一个 Weakmap 实例。然后，将一个 DOM 节点作为键名存入该实例，并将一些附加信息作为键值，一起存放在 WeakMap 里面。这时，WeakMap 里面对<code>element</code>的引用就是弱引用，不会被计入垃圾回收机制。
    </p>
    <p>也就是说，上面的 DOM 节点对象的引用计数是<code>1</code>，而不是<code>2</code>。这时，一旦消除对该节点的引用，它占用的内存就会被垃圾回收机制释放。Weakmap 保存的这个键值对，也会自动消失。
    </p>
    <p>总之，<code>WeakMap</code>的专用场合就是，它的键所对应的对象，可能会在将来消失。<code>WeakMap</code>结构有助于防止内存泄漏。</p>
    <p>注意，WeakMap 弱引用的只是键名，而不是键值。键值依然是正常引用。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">wm</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">WeakMap</span>();
<span class="pl-k">let</span> key <span class="pl-k">=</span> {};
<span class="pl-k">let</span> obj <span class="pl-k">=</span> {foo<span class="pl-k">:</span> <span
                class="pl-c1">1</span>};

<span class="pl-smi">wm</span>.<span class="pl-c1">set</span>(key, obj);
obj <span class="pl-k">=</span> <span class="pl-c1">null</span>;
<span class="pl-smi">wm</span>.<span class="pl-c1">get</span>(key)
<span class="pl-c"><span class="pl-c">//</span> Object {foo: 1}</span></pre>
    </div>
    <p>上面代码中，键值<code>obj</code>是正常引用。所以，即使在 WeakMap 外部消除了<code>obj</code>的引用，WeakMap 内部的引用依然存在。</p>
    <h3><a href="#weakmap-的语法"  class="anchor" id="user-content-weakmap-的语法">

    </a>WeakMap 的语法
    </h3>
    <p>WeakMap 与 Map 在 API
        上的区别主要是两个，一是没有遍历操作（即没有<code>key()</code>、<code>values()</code>和<code>entries()</code>方法），也没有<code>size</code>属性。因为没有办法列出所有键名，某个键名是否存在完全不可预测，跟垃圾回收机制是否运行相关。这一刻可以取到键名，下一刻垃圾回收机制突然运行了，这个键名就没了，为了防止出现不确定性，就统一规定不能取到键名。二是无法清空，即不支持<code>clear</code>方法。因此，<code>WeakMap</code>只有四个方法可用：<code>get()</code>、<code>set()</code>、<code>has()</code>、<code>delete()</code>。
    </p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">wm</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">WeakMap</span>();

<span class="pl-c"><span class="pl-c">//</span> size、forEach、clear 方法都不存在</span>
<span class="pl-smi">wm</span>.<span class="pl-c1">size</span> <span class="pl-c"><span class="pl-c">//</span> undefined</span>
<span class="pl-smi">wm</span>.<span class="pl-smi">forEach</span> <span class="pl-c"><span class="pl-c">//</span> undefined</span>
<span class="pl-smi">wm</span>.<span class="pl-c1">clear</span> <span class="pl-c"><span class="pl-c">//</span> undefined</span></pre>
    </div>
    <h3><a href="#weakmap-的示例"  class="anchor" id="user-content-weakmap-的示例">

    </a>WeakMap 的示例
    </h3>
    <p>WeakMap 的例子很难演示，因为无法观察它里面的引用会自动消失。此时，其他引用都解除了，已经没有引用指向 WeakMap 的键名了，导致无法证实那个键名是不是存在。</p>
    <p>贺师俊老师<a href="https://github.com/ruanyf/es6tutorial/issues/362#issuecomment-292109104">提示</a>，如果引用所指向的值占用特别多的内存，就可以通过
        Node 的<code>process.memoryUsage</code>方法看出来。根据这个思路，网友<a
                href="https://github.com/ruanyf/es6tutorial/issues/362#issuecomment-292451925">vtxf</a>补充了下面的例子。</p>
    <p>首先，打开 Node 命令行。</p>
    <div class="highlight highlight-source-shell">
        <pre>$ node --expose-gc</pre>
    </div>
    <p>上面代码中，<code>--expose-gc</code>参数表示允许手动执行垃圾回收机制。</p>
    <p>然后，执行下面的代码。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c"><span class="pl-c">//</span> 手动执行一次垃圾回收，保证获取的内存使用状态准确</span>
<span class="pl-k">&gt;</span> <span class="pl-c1">global</span>.<span class="pl-en">gc</span>();
<span class="pl-c1">undefined</span>

<span class="pl-c"><span class="pl-c">//</span> 查看内存占用的初始状态，heapUsed 为 4M 左右</span>
<span class="pl-k">&gt;</span> <span class="pl-c1">process</span>.<span class="pl-en">memoryUsage</span>();
{ rss<span class="pl-k">:</span> <span class="pl-c1">21106688</span>,
  heapTotal<span class="pl-k">:</span> <span class="pl-c1">7376896</span>,
  heapUsed<span class="pl-k">:</span> <span class="pl-c1">4153936</span>,
  external<span class="pl-k">:</span> <span class="pl-c1">9059</span> }

<span class="pl-k">&gt;</span> <span class="pl-k">let</span> wm <span class="pl-k">=</span> <span
                class="pl-k">new</span> <span class="pl-en">WeakMap</span>();
<span class="pl-c1">undefined</span>

<span class="pl-c"><span class="pl-c">//</span> 新建一个变量 key，指向一个 5*1024*1024 的数组</span>
<span class="pl-k">&gt;</span> <span class="pl-k">let</span> key <span class="pl-k">=</span> <span
                class="pl-k">new</span> <span class="pl-en">Array</span>(<span class="pl-c1">5</span> <span
                class="pl-k">*</span> <span class="pl-c1">1024</span> <span class="pl-k">*</span> <span class="pl-c1">1024</span>);
<span class="pl-c1">undefined</span>

<span class="pl-c"><span class="pl-c">//</span> 设置 WeakMap 实例的键名，也指向 key 数组</span>
<span class="pl-c"><span class="pl-c">//</span> 这时，key 数组实际被引用了两次，</span>
<span class="pl-c"><span class="pl-c">//</span> 变量 key 引用一次，WeakMap 的键名引用了第二次</span>
<span class="pl-c"><span class="pl-c">//</span> 但是，WeakMap 是弱引用，对于引擎来说，引用计数还是1</span>
<span class="pl-k">&gt;</span> <span class="pl-smi">wm</span>.<span class="pl-c1">set</span>(key, <span
                class="pl-c1">1</span>);
<span class="pl-c1">WeakMap</span> {}

<span class="pl-k">&gt;</span> <span class="pl-c1">global</span>.<span class="pl-en">gc</span>();
<span class="pl-c1">undefined</span>

<span class="pl-c"><span class="pl-c">//</span> 这时内存占用 heapUsed 增加到 45M 了</span>
<span class="pl-k">&gt;</span> <span class="pl-c1">process</span>.<span class="pl-en">memoryUsage</span>();
{ rss<span class="pl-k">:</span> <span class="pl-c1">67538944</span>,
  heapTotal<span class="pl-k">:</span> <span class="pl-c1">7376896</span>,
  heapUsed<span class="pl-k">:</span> <span class="pl-c1">45782816</span>,
  external<span class="pl-k">:</span> <span class="pl-c1">8945</span> }

<span class="pl-c"><span class="pl-c">//</span> 清除变量 key 对数组的引用，</span>
<span class="pl-c"><span class="pl-c">//</span> 但没有手动清除 WeakMap 实例的键名对数组的引用</span>
<span class="pl-k">&gt;</span> key <span class="pl-k">=</span> <span class="pl-c1">null</span>;
<span class="pl-c1">null</span>

<span class="pl-c"><span class="pl-c">//</span> 再次执行垃圾回收</span>
<span class="pl-k">&gt;</span> <span class="pl-c1">global</span>.<span class="pl-en">gc</span>();
<span class="pl-c1">undefined</span>

<span class="pl-c"><span class="pl-c">//</span> 内存占用 heapUsed 变回 4M 左右，</span>
<span class="pl-c"><span class="pl-c">//</span> 可以看到 WeakMap 的键名引用没有阻止 gc 对内存的回收</span>
<span class="pl-k">&gt;</span> <span class="pl-c1">process</span>.<span class="pl-en">memoryUsage</span>();
{ rss<span class="pl-k">:</span> <span class="pl-c1">20639744</span>,
  heapTotal<span class="pl-k">:</span> <span class="pl-c1">8425472</span>,
  heapUsed<span class="pl-k">:</span> <span class="pl-c1">3979792</span>,
  external<span class="pl-k">:</span> <span class="pl-c1">8956</span> }</pre>
    </div>
    <p>上面代码中，只要外部的引用消失，WeakMap 内部的引用，就会自动被垃圾回收清除。由此可见，有了 WeakMap 的帮助，解决内存泄漏就会简单很多。</p>
    <h3><a href="#weakmap-的用途"  class="anchor" id="user-content-weakmap-的用途">

    </a>WeakMap 的用途
    </h3>
    <p>前文说过，WeakMap 应用的典型场合就是 DOM 节点作为键名。下面是一个例子。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">let</span> myElement <span class="pl-k">=</span> <span
            class="pl-c1">document</span>.<span class="pl-c1">getElementById</span>(<span class="pl-s"><span
            class="pl-pds">'</span>logo<span class="pl-pds">'</span></span>);
<span class="pl-k">let</span> myWeakmap <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">WeakMap</span>();

<span class="pl-smi">myWeakmap</span>.<span class="pl-c1">set</span>(myElement, {timesClicked<span class="pl-k">:</span> <span
                class="pl-c1">0</span>});

<span class="pl-smi">myElement</span>.<span class="pl-c1">addEventListener</span>(<span class="pl-s"><span
                class="pl-pds">'</span>click<span class="pl-pds">'</span></span>, <span class="pl-k">function</span>() {
  <span class="pl-k">let</span> logoData <span class="pl-k">=</span> <span class="pl-smi">myWeakmap</span>.<span
                class="pl-c1">get</span>(myElement);
  <span class="pl-smi">logoData</span>.<span class="pl-smi">timesClicked</span><span class="pl-k">++</span>;
}, <span class="pl-c1">false</span>);</pre>
    </div>
    <p>上面代码中，<code>myElement</code>是一个 DOM 节点，每当发生<code>click</code>事件，就更新一下状态。我们将这个状态作为键值放在 WeakMap 里，对应的键名就是<code>myElement</code>。一旦这个
        DOM 节点删除，该状态就会自动消失，不存在内存泄漏风险。</p>
    <p>WeakMap 的另一个用处是部署私有属性。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">const</span> <span class="pl-c1">_counter</span> <span
            class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-en">WeakMap</span>();
<span class="pl-k">const</span> <span class="pl-c1">_action</span> <span class="pl-k">=</span> <span
                class="pl-k">new</span> <span class="pl-en">WeakMap</span>();

<span class="pl-k">class</span> <span class="pl-en">Countdown</span> {
  <span class="pl-en">constructor</span>(<span class="pl-smi">counter</span>, <span class="pl-smi">action</span>) {
    <span class="pl-smi">_counter</span>.<span class="pl-c1">set</span>(<span class="pl-c1">this</span>, counter);
    <span class="pl-smi">_action</span>.<span class="pl-c1">set</span>(<span class="pl-c1">this</span>, action);
  }
  <span class="pl-en">dec</span>() {
    <span class="pl-k">let</span> counter <span class="pl-k">=</span> <span class="pl-smi">_counter</span>.<span
                class="pl-c1">get</span>(<span class="pl-c1">this</span>);
    <span class="pl-k">if</span> (counter <span class="pl-k">&lt;</span> <span class="pl-c1">1</span>) <span
                class="pl-k">return</span>;
    counter<span class="pl-k">--</span>;
    <span class="pl-smi">_counter</span>.<span class="pl-c1">set</span>(<span class="pl-c1">this</span>, counter);
    <span class="pl-k">if</span> (counter <span class="pl-k">===</span> <span class="pl-c1">0</span>) {
      <span class="pl-smi">_action</span>.<span class="pl-c1">get</span>(<span class="pl-c1">this</span>)();
    }
  }
}

<span class="pl-k">const</span> <span class="pl-c1">c</span> <span class="pl-k">=</span> <span
                class="pl-k">new</span> <span class="pl-en">Countdown</span>(<span class="pl-c1">2</span>, () <span
                class="pl-k">=&gt;</span> <span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span
                class="pl-s"><span class="pl-pds">'</span>DONE<span class="pl-pds">'</span></span>));

<span class="pl-smi">c</span>.<span class="pl-en">dec</span>()
<span class="pl-smi">c</span>.<span class="pl-en">dec</span>()
<span class="pl-c"><span class="pl-c">//</span> DONE</span></pre>
    </div>
    <p>上面代码中，<code>Countdown</code>类的两个内部属性<code>_counter</code>和<code>_action</code>，是实例的弱引用，所以如果删除实例，它们也就随之消失，不会造成内存泄漏。
    </p>
</div>
</body>
</html>
